package com.bdth.bdthiss.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.bdth.bdthiss.entity.Bdcard;
import com.bdth.bdthiss.mapper.BdcardMapper;
import com.bdth.bdthiss.service.BdcardService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.bdth.bdthiss.vo.Pagination;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 分理服务卡信息 服务实现类
 * </p>
 *
 * @author LGP
 * @since 2019-09-05
 */
@Service
public class BdcardServiceImpl extends ServiceImpl<BdcardMapper, Bdcard> implements BdcardService {

    @Override
    public Page<Bdcard> listPages(Pagination pagination, Map<String, Object> query) {
        EntityWrapper<Bdcard> entityWrapper = new EntityWrapper<>();
        String name = (String)query.get("name");
        if(name!=null && !name.trim().equals(""))
            entityWrapper.like("ctname",name);
        String code = (String)query.get("code");
        if(code!=null && !code.trim().equals(""))
            entityWrapper.like("ctcode",code);
        String start = (String)query.get("start");
        if(start!=null && !start.trim().equals(""))
            entityWrapper.ge("crtime",start);
        String end = (String)query.get("end");
        if(end!=null && !end.trim().equals(""))
            entityWrapper.le("crtime",end);
        try {
            Integer opid = (Integer) query.get("opid");
            if (opid != null)
                entityWrapper.in("opid", opid+"");
        }catch (Exception ex){
            String opid = (String) query.get("opid");
            if (opid != null)
                entityWrapper.in("opid", opid);
        }
        if(pagination.getOrder()!=null) {
            if (pagination.getOrder().toLowerCase().equals("asc")){
                entityWrapper.orderBy(pagination.getSort(), true);
            }else{
                entityWrapper.orderBy(pagination.getSort(), false);
            }
        }
        Page<Bdcard> page = new Page<>(pagination.getPage()==null?1:pagination.getPage(),
                pagination.getRows()==null?20:pagination.getRows());

        List<Bdcard> items = baseMapper.selectPage(page,entityWrapper);
        page.setRecords(items);
        return page;
    }
}
